//CourseDetailServlet.java
package course;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import course.Course;
import course.DBcon;

import org.apache.log4j.Logger;

 public  class CourseDetailServlet extends HttpServlet{
	
	private static org.apache.log4j.Logger log = Logger.getLogger(CourseDetailServlet.class);

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

    	HttpSession session = req.getSession();
		String loguser = (String) session.getAttribute("loguser");
		String crid = req.getParameter("cdetail");
		session.setAttribute("cdetail", crid);
		
	
		if (loguser == null) {
			session.setAttribute("cdetail",crid);
		//	System.out.println("login :" + loguser);
		//	System.out.println("crid =" + crid);
			req.getRequestDispatcher("noprior.jsp").forward(req, resp);
		} else {
		
			PreparedStatement ps = null;
			Connection con = null;
	    try {
	    	try {
				con = DBcon.getConnection();
	    		log.info("PD-Connected!" );

	    		ps = con.prepareStatement("select * from course where cr_id=?",
						ResultSet.TYPE_FORWARD_ONLY,
						ResultSet.CONCUR_UPDATABLE);

	    		try{
	    			ps.setString(1, crid);
	    			ResultSet rs= ps.executeQuery();
	    			if (rs.next()){
	    				String cr_id = rs.getString(1);
	    				String cr_descr = rs.getString(2);
	    				String cr_type = rs.getString(3);
	    				Date sd = rs.getDate(4);
	    				Date ed = rs.getDate(5);
	    				Integer cr_hours =rs.getInt(6);
	    				Integer cr_stucnt = rs.getInt(7);
	    				String cr_remark = rs.getString(8);
	    				Integer cr_obj = rs.getInt(9);
	    				String cr_location = rs.getString(10);
	    				double cr_fee = rs.getDouble(11);
	    				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
	    				String cr_startdate = sf.format(sd);
	    				String cr_enddate = sf.format(ed);
	    				con.commit();

	    				Course cou = new Course(cr_id, cr_descr, cr_type, cr_startdate, cr_enddate, cr_hours, cr_stucnt, cr_remark, cr_obj, cr_location, cr_fee);
	    				req.setAttribute("cou", cou);
	    				req.getRequestDispatcher("coursedetail.jsp").forward(req, resp);
	    			}
	    		}finally{
	    			con.commit();
	    			ps.close();
	    		}
	    	}finally {
	    		con.close();
	    	}
	    }catch (SQLException e){
	    	throw new RuntimeException (e);
	    } finally {
	    	try {
	    		ps.close();
	    		con.close();
	    	} catch (SQLException e) {
	    		e.printStackTrace();
	    		log.debug("PD-update fail!!");
	    	}
	    }
//		}
		}
	}	
	

	
	 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	 throws ServletException, IOException {
	
		String crid = req.getParameter("crid");
		String crdescr = req.getParameter("crdescr");
		String crtype = req.getParameter("crtype");
		String crstartdate = req.getParameter("crstartdate");
		String crenddate = req.getParameter("crenddate");
		String crhours = req.getParameter("crhours");
		String crstucnt = req.getParameter("crstucnt");
		String crremark = req.getParameter("crremark");
		String crobj = req.getParameter("crobj");
		String crlocation = req.getParameter("crlocation");
		String crfee = req.getParameter("crfee");
		String errorcode = null;
		int upflag = 0;
		
		PreparedStatement ps = null;
		Connection con = null;
			
		if (crid.equals("")) {
	    	 System.out.println("crid is null");
	    	 errorcode="UA-001";
		} else {
			if (crdescr.equals("")) {
				System.out.println("crdescr is null");
				errorcode="UA-002";
			}
			else
				if (crtype.equals("")) {
					System.out.println("crtype is null");
					errorcode="UA-003";
				}
		}
		
	    	HttpSession session = req.getSession();
	    	session.setAttribute("crid", crid);
			session.setAttribute("crdescr", crdescr);
	    	session.setAttribute("crtype", crtype);
			session.setAttribute("crstartdate", crstartdate);
	    	session.setAttribute("crenddate", crenddate);
			session.setAttribute("crhours", crhours);
	    	session.setAttribute("crstucnt", crstucnt);
			session.setAttribute("crremark", crremark);
	    	session.setAttribute("crobj", crobj);
			session.setAttribute("crlocation", crlocation);
			session.setAttribute("crfee", crfee);
			
			
			session.setAttribute("error", errorcode);

		if (errorcode != null) {
			System.out.println("errorcode :... " +errorcode);
			/*
	    	HttpSession session = req.getSession();
	    	session.setAttribute("areacode", areacode);
			session.setAttribute("areaname", areaname);
			session.setAttribute("error", errorcode);
			*/
			log.error("errorcode : " + errorcode);
			resp.sendRedirect("upareafail.jsp");
		} else {
			try {
			
				con = DBcon.getConnection();
				log.info("UA-Connected!" );
/*				
				ps = con.prepareStatement("select areacode, areaname from areatbl " +
						"where areacode=(?)");
			    ps.setString(1, areacode);
			    ResultSet rs = ps.executeQuery();
				log.info("UA-area" + areacode + "is selected" );
			    while (rs.next()){
			    	upflag = 1;
			    }
			    if (upflag == 1) {
			    	ps = con.prepareStatement("update areatbl set areaname=? where areacode=?");
				    ps.setString(1, areaname);
					ps.setString(2, areacode);
			    } else {
			    	ps = con.prepareStatement("insert into areatbl values (?,?)");
				    ps.setString(1, areacode);
					ps.setString(2, areaname);
					log.info("UA-insert area" + areacode);
			    }	     
		        int n =ps.executeUpdate();
		        con.commit();
		        System.out.println("AreaCode :" + areacode);
		        System.out.println("AreaName   :" + areaname);
				log.info("UA-update/insert OK" + areacode);
				resp.sendRedirect("upareaok.jsp");
*/				
			} catch (SQLException e) {
				e.printStackTrace();
				log.debug("UI-update fail!!");
			} finally {
				try {
					ps.close();
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
					log.debug("UI-update fail!!");
				}
			}
		}
	}
}
